﻿// RS232Dlg.cpp: 实现文件
//

#include "pch.h"
#include "ProjectAA.h"
#include "RS232Dlg.h"
#include "afxdialogex.h"


// CRS232Dlg 对话框

IMPLEMENT_DYNAMIC(CRS232Dlg, CDialogEx)

CRS232Dlg::CRS232Dlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_RS232, pParent)
	, m_bPassRfid(FALSE)
	, m_nCom1(0)
	, m_nPort1(0)
	, m_nCom2(0)
	, m_nPort2(0)
	, m_nComNo4(0)
	, m_nComPort4(0)
	, m_nCom3(0)
	, m_nPort3(0)
	, m_nCom4(0)
	, m_nPort4(0)
	, PlasmaComNo(0)
	, PlasmaPort(0)
	, PlasmaCheckFlag(FALSE)
	, PlasmaPowerMax(0)
	, PlasmaPowerMin(0)
	, PlasmaCheckDelay(0)
{

}

CRS232Dlg::~CRS232Dlg()
{
}

void CRS232Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Check(pDX, IDC_CHECK1, m_bPassRfid);
	DDX_Text(pDX, IDC_EDIT_COM1, m_nCom1);
	DDX_Text(pDX, IDC_EDIT_PORT1, m_nPort1);
	DDX_Text(pDX, IDC_EDIT_COM2, m_nCom2);
	DDX_Text(pDX, IDC_EDIT_PORT3, m_nPort2);
	DDX_Text(pDX, IDC_EDIT_COM3, m_nComNo4);
	DDX_Text(pDX, IDC_EDIT_PORT4, m_nComPort4);
	DDX_Control(pDX, IDC_COMBO1, m_comboPallent);
	DDX_Text(pDX, IDC_EDIT_COM4, m_nCom3);
	DDX_Text(pDX, IDC_EDIT_PORT5, m_nPort3);
	DDX_Text(pDX, IDC_EDIT_COM5, m_nCom4);
	DDX_Text(pDX, IDC_EDIT_PORT6, m_nPort4);
	DDX_Text(pDX, IDC_EDIT_COM6, PlasmaComNo);
	DDX_Text(pDX, IDC_EDIT_PORT10, PlasmaPort);
	DDX_Check(pDX, IDC_CHECK3, PlasmaCheckFlag);
	DDX_Text(pDX, IDC_EDIT1, PlasmaPowerMax);
	DDX_Text(pDX, IDC_EDIT18, PlasmaPowerMin);
	DDX_Text(pDX, IDC_EDIT19, PlasmaCheckDelay);
}


BEGIN_MESSAGE_MAP(CRS232Dlg, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON1, &CRS232Dlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CRS232Dlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON6, &CRS232Dlg::OnBnClickedButton6)
	ON_BN_CLICKED(IDC_BUTTON3, &CRS232Dlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON4, &CRS232Dlg::OnBnClickedButton4)
	ON_BN_CLICKED(IDC_BUTTON5, &CRS232Dlg::OnBnClickedButton5)
	ON_BN_CLICKED(IDC_BUTTON10, &CRS232Dlg::OnBnClickedButton10)
	ON_BN_CLICKED(IDC_BUTTON26, &CRS232Dlg::OnBnClickedButton26)
	ON_BN_CLICKED(IDC_BUTTON27, &CRS232Dlg::OnBnClickedButton27)
	ON_BN_CLICKED(IDC_BUTTON28, &CRS232Dlg::OnBnClickedButton28)
	ON_BN_CLICKED(IDC_BUTTON29, &CRS232Dlg::OnBnClickedButton29)
	ON_BN_CLICKED(IDC_BUTTON7, &CRS232Dlg::OnBnClickedButton7)
	ON_BN_CLICKED(IDC_BUTTON31, &CRS232Dlg::OnBnClickedButton31)
	ON_BN_CLICKED(IDC_BUTTON32, &CRS232Dlg::OnBnClickedButton32)
	ON_BN_CLICKED(IDC_BUTTON33, &CRS232Dlg::OnBnClickedButton33)
	ON_BN_CLICKED(IDC_BUTTON35, &CRS232Dlg::OnBnClickedButton35)
	ON_BN_CLICKED(IDC_BUTTON37, &CRS232Dlg::OnBnClickedButton37)
	ON_BN_CLICKED(IDC_BUTTON39, &CRS232Dlg::OnBnClickedButton39)
	ON_BN_CLICKED(IDC_BUTTON8, &CRS232Dlg::OnBnClickedButton8)
END_MESSAGE_MAP()


// CRS232Dlg 消息处理程序


BOOL CRS232Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	m_comboPallent.ResetContent();
	for (int i = 0; i < 20; i++) {
		CString cstr;
		cstr.Format(L"%02d", i + 1);
		m_comboPallent.AddString(cstr.GetBuffer());
	}
	m_comboPallent.SetCurSel(0);

	ReadConfigIni();

	return TRUE;
}


void CRS232Dlg::OnCancel()
{
	// TODO: 在此添加专用代码和/或调用基类

	CDialogEx::OnCancel();
}

void CRS232Dlg::ReadConfigIni()
{
	m_nCom1 = gSystemMgr.m_nComNo1;
	m_nPort1 = gSystemMgr.m_nComPort1;
	m_nCom2 = gSystemMgr.m_nComNo2;
	m_nPort2 = gSystemMgr.m_nComPort2;
	m_bPassRfid = gSystemMgr.m_nPassRfidFlag;
	m_nComNo4 = gSystemMgr.m_nComNo_Scale;
	m_nComPort4 = gSystemMgr.m_nComPort_Scale;

	m_nCom3 = gSystemMgr.m_nComNo3;
	m_nPort3 = gSystemMgr.m_nComPort3;
	m_nCom4 = gSystemMgr.m_nComNo4;
	m_nPort4 = gSystemMgr.m_nComPort4;

	PlasmaComNo = gSystemMgr.PlasmaComNo;
	PlasmaPort = gSystemMgr.PlasmaPort;
	PlasmaCheckFlag = gSystemMgr.PlasmaCheckFlag;
	PlasmaPowerMax = gSystemMgr.PlasmaPowerMax;
	PlasmaPowerMin = gSystemMgr.PlasmaPowerMin;
	PlasmaCheckDelay = gSystemMgr.PlasmaCheckDelay;

	UpdateData(FALSE);
}

void CRS232Dlg::SaveValueToConfig()
{
	UpdateData(TRUE);
	CString cstr;
	gSystemMgr.m_nComNo1 = m_nCom1;
	gSystemMgr.m_nComPort1 = m_nPort1;
	gSystemMgr.m_nComNo2 = m_nCom2;
	gSystemMgr.m_nComPort2 = m_nPort2;
	gSystemMgr.m_nPassRfidFlag = m_bPassRfid;
	gSystemMgr.m_nComNo_Scale = m_nComNo4;
	gSystemMgr.m_nComPort_Scale = m_nComPort4;

	gSystemMgr.m_nComNo3 = m_nCom3;
	gSystemMgr.m_nComPort3 = m_nPort3;
	gSystemMgr.m_nComNo4 = m_nCom4;
	gSystemMgr.m_nComPort4 = m_nPort4;

	gSystemMgr.PlasmaComNo = PlasmaComNo;
	gSystemMgr.PlasmaPort = PlasmaPort;
	gSystemMgr.PlasmaCheckFlag = PlasmaCheckFlag;
	gSystemMgr.PlasmaPowerMax = PlasmaPowerMax;
	gSystemMgr.PlasmaPowerMin = PlasmaPowerMin;
	gSystemMgr.PlasmaCheckDelay = PlasmaCheckDelay;

	cstr.Format(L"%d", gSystemMgr.PlasmaComNo);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"RS232", L"PlasmaComNo", cstr);
	cstr.Format(L"%d", gSystemMgr.PlasmaPort);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"RS232", L"PlasmaPort", cstr);
	cstr.Format(L"%d", gSystemMgr.PlasmaCheckFlag);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"RS232", L"PlasmaCheckFlag", cstr);
	cstr.Format(L"%d", gSystemMgr.PlasmaPowerMax);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"RS232", L"PlasmaPowerMax", cstr);
	cstr.Format(L"%d", gSystemMgr.PlasmaPowerMin);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"RS232", L"PlasmaPowerMin", cstr);
	cstr.Format(L"%d", gSystemMgr.PlasmaCheckDelay);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"RS232", L"PlasmaCheckDelay", cstr);


	cstr.Format(L"%d", gSystemMgr.m_nComNo1);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"RS232", L"m_nComNo1", cstr);

	cstr.Format(L"%d", gSystemMgr.m_nComPort1);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"RS232", L"m_nComPort1", cstr);

	cstr.Format(L"%d", gSystemMgr.m_nComNo2);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"RS232", L"m_nComNo2", cstr);

	cstr.Format(L"%d", gSystemMgr.m_nComPort2);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"RS232", L"m_nComPort2", cstr);

	cstr.Format(L"%d", gSystemMgr.m_nComNo3);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"RS232", L"m_nComNo3", cstr);

	cstr.Format(L"%d", gSystemMgr.m_nComPort3);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"RS232", L"m_nComPort3", cstr);

	cstr.Format(L"%d", gSystemMgr.m_nComNo4);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"RS232", L"m_nComNo4", cstr);

	cstr.Format(L"%d", gSystemMgr.m_nComPort4);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"RS232", L"m_nComPort4", cstr);

	cstr.Format(L"%d", gSystemMgr.m_nPassRfidFlag);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"RS232", L"m_nPassRfidFlag", cstr);

	cstr.Format(L"%d", gSystemMgr.m_nComNo_Scale);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"RS232", L"m_nComNo_Scale", cstr);

	cstr.Format(L"%d", gSystemMgr.m_nComPort_Scale);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"RS232", L"m_nComPort_Scale", cstr);

	gSystemLog.DisplayLogAndSave(L"RS232保存数据完成", LV_EVENT);
}





void CRS232Dlg::OnBnClickedButton1()
{
	SaveValueToConfig();
}


void CRS232Dlg::OnBnClickedButton2()
{
	if (gSystemMgr.m_cRfidComm1.get_com_port() == NULL) {
		int err = gSystemMgr.m_cRfidComm1.com_open(gSystemMgr.m_nComNo1, gSystemMgr.m_nComPort1, EVENPARITY);
		if (err != 1) {
			CString cstr;
			cstr.Format(L"Com1 Error %d", err);
			gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
			gSystemMgr.Com1ConnectFlag = 0;
		}
		else
			gSystemMgr.Com1ConnectFlag = 1;
	}
}


void CRS232Dlg::OnBnClickedButton6()
{
	if (gSystemMgr.m_cRfidComm2.get_com_port() == NULL) {
		int err = gSystemMgr.m_cRfidComm2.com_open(gSystemMgr.m_nComNo2, gSystemMgr.m_nComPort2, EVENPARITY);
		if (err != 1) {
			CString cstr;
			cstr.Format(L"Com2 Error %d", err);
			gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
			gSystemMgr.Com2ConnectFlag = 0;
		}
		else
			gSystemMgr.Com2ConnectFlag = 1;
	}
}


void CRS232Dlg::OnBnClickedButton3()
{
	gSystemMgr.m_nButtonValue = 30;
}


void CRS232Dlg::OnBnClickedButton4()
{
	gSystemMgr.m_nButtonValue = 31;
}


void CRS232Dlg::OnBnClickedButton5()
{
	gSystemMgr.m_nButtonValue = 32;
}


void CRS232Dlg::OnBnClickedButton10()
{
	gSystemMgr.m_nButtonValue = 33;
}


void CRS232Dlg::OnBnClickedButton26()
{
	gSystemMgr.m_nButtonValue = 34;
}


void CRS232Dlg::OnBnClickedButton27()
{
	gSystemMgr.m_nButtonValue = 35;
}



void CRS232Dlg::OnBnClickedButton28()
{
	gSystemMgr.m_nButtonValue = 36;
}


void CRS232Dlg::OnBnClickedButton29()
{
	gSystemMgr.m_nButtonValue = 37;
}


void CRS232Dlg::OnBnClickedButton7()
{
	BYTE Pallent_00[8] = { 0x02, 0x06, 0x00, 0x0A, 0x00, 0x01, 0x68, 0x3B };
	BYTE Pallent_01[8] = { 0x02, 0x06, 0x00, 0x0A, 0x00, 0x02, 0x28, 0x3A };
	BYTE Pallent_02[8] = { 0x02, 0x06, 0x00, 0x0A, 0x00, 0x03, 0xE9, 0xFA };
	BYTE Pallent_03[8] = { 0x02, 0x06, 0x00, 0x0A, 0x00, 0x04, 0xA8, 0x38 };
	BYTE Pallent_04[8] = { 0x02, 0x06, 0x00, 0x0A, 0x00, 0x05, 0x69, 0xF8 };
	BYTE Pallent_05[8] = { 0x02, 0x06, 0x00, 0x0A, 0x00, 0x06, 0x29, 0xF9 };
	BYTE Pallent_06[8] = { 0x02, 0x06, 0x00, 0x0A, 0x00, 0x07, 0xE8, 0x39 };
	BYTE Pallent_07[8] = { 0x02, 0x06, 0x00, 0x0A, 0x00, 0x08, 0xA8, 0x3D };
	BYTE Pallent_08[8] = { 0x02, 0x06, 0x00, 0x0A, 0x00, 0x09, 0x69, 0xFD };
	BYTE Pallent_09[8] = { 0x02, 0x06, 0x00, 0x0A, 0x00, 0x0A, 0x29, 0xFC };
	BYTE Pallent_10[8] = { 0x02, 0x06, 0x00, 0x0A, 0x00, 0x0B, 0xE8, 0x3C };
	BYTE Pallent_11[8] = { 0x02, 0x06, 0x00, 0x0A, 0x00, 0x0C, 0xA9, 0xFE };
	BYTE Pallent_12[8] = { 0x02, 0x06, 0x00, 0x0A, 0x00, 0x0D, 0x68, 0x3E };
	BYTE Pallent_13[8] = { 0x02, 0x06, 0x00, 0x0A, 0x00, 0x0E, 0x28, 0x3F };
	BYTE Pallent_14[8] = { 0x02, 0x06, 0x00, 0x0A, 0x00, 0x0F, 0xE9, 0xFF };
	BYTE Pallent_15[8] = { 0x02, 0x06, 0x00, 0x0A, 0x00, 0x10, 0xA8, 0x37 };
	BYTE Pallent_16[8] = { 0x02, 0x06, 0x00, 0x0A, 0x00, 0x11, 0x69, 0xF7 };
	BYTE Pallent_17[8] = { 0x02, 0x06, 0x00, 0x0A, 0x00, 0x12, 0x29, 0xF6 };
	BYTE Pallent_18[8] = { 0x02, 0x06, 0x00, 0x0A, 0x00, 0x13, 0xE8, 0x36 };
	BYTE Pallent_19[8] = { 0x02, 0x06, 0x00, 0x0A, 0x00, 0x14, 0xA9, 0xF4 };

	char output[150];

	if (IDYES == AfxMessageBox(L"确定 写入当前托盘号 吗？", MB_YESNO))
	{
		memset(output, 0, sizeof(output));
		switch (m_comboPallent.GetCurSel()) {
		case 0:gSystemMgr.m_cRfidComm1.send_data_feedback((BYTE*)Pallent_00, sizeof(Pallent_00), (BYTE*)output, sizeof(output)); break;
		case 1:gSystemMgr.m_cRfidComm1.send_data_feedback((BYTE*)Pallent_01, sizeof(Pallent_01), (BYTE*)output, sizeof(output)); break;
		case 2:gSystemMgr.m_cRfidComm1.send_data_feedback((BYTE*)Pallent_02, sizeof(Pallent_02), (BYTE*)output, sizeof(output)); break;
		case 3:gSystemMgr.m_cRfidComm1.send_data_feedback((BYTE*)Pallent_03, sizeof(Pallent_03), (BYTE*)output, sizeof(output)); break;
		case 4:gSystemMgr.m_cRfidComm1.send_data_feedback((BYTE*)Pallent_04, sizeof(Pallent_04), (BYTE*)output, sizeof(output)); break;
		case 5:gSystemMgr.m_cRfidComm1.send_data_feedback((BYTE*)Pallent_05, sizeof(Pallent_05), (BYTE*)output, sizeof(output)); break;
		case 6:gSystemMgr.m_cRfidComm1.send_data_feedback((BYTE*)Pallent_06, sizeof(Pallent_06), (BYTE*)output, sizeof(output)); break;
		case 7:gSystemMgr.m_cRfidComm1.send_data_feedback((BYTE*)Pallent_07, sizeof(Pallent_07), (BYTE*)output, sizeof(output)); break;
		case 8:gSystemMgr.m_cRfidComm1.send_data_feedback((BYTE*)Pallent_08, sizeof(Pallent_08), (BYTE*)output, sizeof(output)); break;
		case 9:gSystemMgr.m_cRfidComm1.send_data_feedback((BYTE*)Pallent_09, sizeof(Pallent_09), (BYTE*)output, sizeof(output)); break;
		case 10:gSystemMgr.m_cRfidComm1.send_data_feedback((BYTE*)Pallent_10, sizeof(Pallent_10), (BYTE*)output, sizeof(output)); break;
		case 11:gSystemMgr.m_cRfidComm1.send_data_feedback((BYTE*)Pallent_11, sizeof(Pallent_11), (BYTE*)output, sizeof(output)); break;
		case 12:gSystemMgr.m_cRfidComm1.send_data_feedback((BYTE*)Pallent_12, sizeof(Pallent_12), (BYTE*)output, sizeof(output)); break;
		case 13:gSystemMgr.m_cRfidComm1.send_data_feedback((BYTE*)Pallent_13, sizeof(Pallent_13), (BYTE*)output, sizeof(output)); break;
		case 14:gSystemMgr.m_cRfidComm1.send_data_feedback((BYTE*)Pallent_14, sizeof(Pallent_14), (BYTE*)output, sizeof(output)); break;
		case 15:gSystemMgr.m_cRfidComm1.send_data_feedback((BYTE*)Pallent_15, sizeof(Pallent_15), (BYTE*)output, sizeof(output)); break;
		case 16:gSystemMgr.m_cRfidComm1.send_data_feedback((BYTE*)Pallent_16, sizeof(Pallent_16), (BYTE*)output, sizeof(output)); break;
		case 17:gSystemMgr.m_cRfidComm1.send_data_feedback((BYTE*)Pallent_17, sizeof(Pallent_17), (BYTE*)output, sizeof(output)); break;
		case 18:gSystemMgr.m_cRfidComm1.send_data_feedback((BYTE*)Pallent_18, sizeof(Pallent_18), (BYTE*)output, sizeof(output)); break;
		case 19:gSystemMgr.m_cRfidComm1.send_data_feedback((BYTE*)Pallent_19, sizeof(Pallent_19), (BYTE*)output, sizeof(output)); break;
		}
	}
}


void CRS232Dlg::OnBnClickedButton31()
{
	gSystemMgr.m_nButtonValue = 40;
}


void CRS232Dlg::OnBnClickedButton32()
{
	gSystemMgr.m_nButtonValue = 41;
}


void CRS232Dlg::OnBnClickedButton33()
{
	gSystemMgr.m_nButtonValue = 42;
}


void CRS232Dlg::OnBnClickedButton35()
{
	gSystemMgr.m_nButtonValue = 43;
}


void CRS232Dlg::OnBnClickedButton37()
{
	gSystemMgr.m_nButtonValue = 44;
}


void CRS232Dlg::OnBnClickedButton39()
{
	gSystemMgr.m_nButtonValue = 45;
}

void CRS232Dlg::OnBnClickedButton8()
{
	std::thread t([]()
		{

			gSystemMgr.m_nWeighFlag = 1;
			gSystemLog.DisplayLogAndSave(L"gSystemMgr.m_nWeighFlag = 1;", LV_EVENT);
			Sleep(3000);
			gSystemMgr.m_nWeighFlag = 0;
			gSystemLog.DisplayLogAndSave(L"gSystemMgr.m_nWeighFlag = 0;", LV_EVENT);

		});
	t.detach();
}
